<?php
/**
 * Constants section
 */
	define ( 'DS', DIRECTORY_SEPARATOR );
	define ( 'PS', PATH_SEPARATOR );
	define ( 'HOME_DIR', dirname ( __FILE__ ) . DS );
	define ( 'DEV_MODE', TRUE );
	define ( 'FORMS_FILES', HOME_DIR . 'config/forms/' );

	if(DEV_MODE){
		ini_set('display_errors','on');
		error_reporting ( E_ALL | E_STRICT );
	}	
	
/**
 * Set include pathes
 */
	$paths [] = HOME_DIR . 'app' . DS;
	$paths [] = HOME_DIR . 'Libs' . DS;
	$paths [] = HOME_DIR . 'app/controllers' . DS;
	$app_path = implode ( PS, $paths );
	set_include_path ( $app_path . PS . get_include_path () );

/**
 * Function Section
 */
	//To prevent php warning from smarty compiler
	if (function_exists ( "date_default_timezone_set" ) and function_exists ( "date_default_timezone_get" )) {
		@date_default_timezone_set ( @date_default_timezone_get () );
	}
	
	//Define autoload function to get rid of includes
	include_once ('Libs/Zend/Loader.php');
	function __autoload($classname) {
		Zend_Loader::loadClass ( $classname );
	}
	
/**
 * Db section  
 */
	$params = array ('host'     => 'localhost'
	                 ,'username' => 'root'
	                 ,'password' => ''
	                 ,'dbname'   => 'demo'
	                 );
	
	//$db = Zend_Db::factory('PDO_MYSQL', $params); 
	$db = new Bel_Db_Adapter_Pdo_Mysql($params); //adapter with cached DESCRIBE TABLE queries
	$db->setFetchMode(Zend_Db::FETCH_OBJ);
	
	if(DEV_MODE){
		$profiler = new Zend_Db_Profiler_Firebug('All DB Queries');
		$profiler->setEnabled(true);
		$db->setProfiler($profiler);
	}	
	                 
	$db->query('SET CHARACTER SET utf8'); 
	$db->query('SET NAMES utf8');
	
	Zend_Registry::set('db', $db);
	define('TABLE_PREFIX','PREFIX_');

/**
 * Cache section
 */
	$frontendOptions = array ('lifetime' => 28800, 'automatic_serialization' => true );
	$backendOptions = array ('cache_dir' => HOME_DIR . 'tmp/cache' );
	$cache = Zend_Cache::factory('Bel_Cache', 'File', $frontendOptions, $backendOptions, true,false,true); //Don't caching data if DEV_MODE = true
	Zend_Registry::set('cache', $cache);		
	
/**
 * Front Controller init
 */	
	$controller = Zend_Controller_Front::getInstance ();
	if (DEV_MODE) {
		$controller->throwExceptions ( true );
	}	
	
/**
 * View section
 */
	/* disable standart Zend View - begin*/
	Zend_Controller_Action_HelperBroker::removeHelper ( 'viewRenderer' );
	$controller->setParam ( 'noViewRenderer', true );
	/* disable standart Zend View - end */
	include_once HOME_DIR.'Libs/smarty/Smarty.class.php';
	$view = new Smarty ( );	
	if(strpos($_SERVER['REQUEST_URI'],'admin')===1){ //if requested module is 'admin' then set theme to admin
		$view->compile_dir = HOME_DIR . 'tmp/templates_c/admin';
		$view->template_dir = HOME_DIR . 'app/views/admin/templates';
		$view->assign ( 'theme', 'app/views/admin/' );
	}else{
		$view->compile_dir = HOME_DIR . 'tmp/templates_c/default';
		$view->template_dir = HOME_DIR . 'app/views/default/templates';
		$view->assign ( 'theme', 'app/views/default/' );	
	}
	Zend_Registry::set ( 'view', $view );
	
/**
 * ACL Resourses setup 
 */	
	$groups = new Models_Usermanagement_Groups();
	$modules = new Models_Usermanagement_Modules();
	$group_id = $groups->getCurrentGroupId();
		
	if (! ($resources = $cache->load('acl_resources' . $group_id))) {
	    $resources = $modules->getAllAclResources($group_id);
	    $cache->save($resources);
	}
	
	if (! ($acl = $cache->load('acl_' . $group_id))) {
	    $acl = new Zend_Acl();
	    $acl->addRole(new Zend_Acl_Role($group_id));
	    foreach ($resources as $resource) {
	        if (! $acl->has($resource->resource_name)) {
	            $acl->add(new Zend_Acl_Resource($resource->resource_name));
	            if ((int)$resource->group===(int)$group_id) {
	                $acl->allow($group_id, $resource->resource_name);
	            } else {
	                $acl->deny($group_id, $resource->resource_name);
	            }
	        }
	    }
	    $cache->save($acl);
	}
	
	Zend_Registry::set('acl', $acl);
/**
 * Plugins section
 */
	$controller->registerPlugin (new Bel_Acl_Plugin());
	$controller->registerPlugin (new Helpers_Content_TitlePlugin());
	$controller->registerPlugin (new Bel_Router_Plugin());
	$controller->registerPlugin (new Bel_Layout_Plugin(),101);
	
/**
 * Dispatching 
 */	
	if (DEV_MODE) {
		$controller->throwExceptions ( true );
	}
	
	try {
		$response = $controller->dispatch ();
	} catch ( Exception $e ) {
		
		$request = $controller->getRequest ();
		$controller_dir = $controller->getControllerDirectory ( $request->getModuleName () );
		$view->template_dir = 'views';
		
		$view->assign ( 'exception', $e );
		$view->assign ( 'request', $request );
		$view->assign ( 'controller_dir', $controller_dir );
		$view->display ( 'error.tpl' );
	}